home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlutil.zip / SAVEDIR.C < prev    next >
C/C++ Source or Header  |  1990-06-19  |  3KB  |  124 lines

  1. /* savedir.c -- save the list of files in a directory in a string
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@ai.mit.edu>. */
  19.  
  20. #include <sys/types.h>
  21. #ifdef DIRENT
  22. #include <dirent.h>
  23. #define direct dirent
  24. #define NLENGTH(direct) (strlen((direct)->d_name))
  25. #else
  26. #define NLENGTH(direct) ((direct)->d_namlen)
  27. #ifdef USG
  28. #ifdef SYSNDIR
  29. #include <sys/ndir.h>
  30. #else
  31. #include <ndir.h>
  32. #endif
  33. #else
  34. #include <sys/dir.h>
  35. #endif
  36. #endif
  37.  
  38. #ifdef STDC_HEADERS
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #else
  42. char *malloc ();
  43. char *realloc ();
  44. int strlen ();
  45. #ifndef NULL
  46. #define NULL 0
  47. #endif
  48. #endif
  49.  
  50. char *stpcpy ();
  51.  
  52. /* Return a freshly allocated string containing the filenames
  53.    in directory DIR, separated by '\0' characters;
  54.    the end is marked by two '\0' characters in a row.
  55.    NAME_SIZE is the number of bytes to initially allocate
  56.    for the string; it will be enlarged as needed.
  57.    Return NULL if DIR cannot be opened or if out of memory. */
  58.  
  59. char *
  60. savedir (dir, name_size)
  61.      char *dir;
  62.      unsigned name_size;
  63. {
  64.   DIR *dirp;
  65.   struct direct *dp;
  66.   char *name_space;
  67.   char *namep;
  68.  
  69.   dirp = opendir (dir);
  70.   if (dirp == NULL)
  71.     return NULL;
  72.  
  73.   name_space = (char *) malloc (name_size);
  74.   if (name_space == NULL)
  75.     return NULL;
  76.   namep = name_space;
  77.  
  78.   while ((dp = readdir (dirp)) != NULL)
  79.     {
  80.       /* Skip "." and ".." (some NFS filesystems' directories lack them). */
  81.       if (dp->d_name[0] != '.'
  82.       || (dp->d_name[1] != '\0'
  83.           && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
  84.     {
  85.       unsigned size_needed = (namep - name_space) + NLENGTH (dp) + 2;
  86.  
  87.       if (size_needed > name_size)
  88.         {
  89.           char *new_name_space;
  90.  
  91.           while (size_needed > name_size)
  92.         name_size += 1024;
  93.  
  94.           new_name_space = realloc (name_space, name_size);
  95.           if (new_name_space == NULL)
  96.         {
  97.           closedir (dirp);
  98.           return NULL;
  99.         }
  100.           namep += new_name_space - name_space;
  101.           name_space = new_name_space;
  102.         }
  103.       namep = stpcpy (namep, dp->d_name) + 1;
  104.     }
  105.     }
  106.   *namep = '\0';
  107.   closedir (dirp);
  108.   return name_space;
  109. }
  110.  
  111. /* Copy SOURCE into DEST, stopping after copying the first '\0', and
  112.    return a pointer to the '\0' at the end of DEST;
  113.    in other words, return DEST + strlen (SOURCE). */
  114.  
  115. char *
  116. stpcpy (dest, source)
  117.      char *dest;
  118.      char *source;
  119. {
  120.   while ((*dest++ = *source++) != '\0')
  121.     /* Do nothing. */ ;
  122.   return dest - 1;
  123. }
  124.